package org.chaos.sekiro.app.internal.dao

import org.chaos.sekiro.app.internal.model.Person
import org.chaos.sekiro.app.internal.model.PersonType
import org.chaos.sekiro.common.ktorm.AuditTable
import org.ktorm.database.Database
import org.ktorm.dsl.eq
import org.ktorm.entity.find
import org.ktorm.entity.sequenceOf
import org.ktorm.schema.*
import org.springframework.stereotype.Component

object People : AuditTable<Person>("people"){
    val id = long("id").primaryKey().bindTo { it.id }
    val name = varchar("name").bindTo { it.name }
    val age = int("age").bindTo { it.age }
    val type = enum<PersonType>("type").bindTo { it.type }
}

val Database.people get() = this.sequenceOf(People)

@Component
class PersonDao(
    private val database: Database
) {
    fun findById(id: Long) = database.people.find { it.id eq id }

}